﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using WPFExcelReport.Helpers;
using Microsoft.AspNet.SignalR;
using System.Collections.Generic;
using System.Threading.Tasks;
using WPFExcelReport.Helpers;
using WPFExcelReport;
using System.Threading;
using System.Web;
namespace INTEXReportSrv
{
    public class MyHub : Hub
    {
        public static class UserHandler
        {

            public static Dictionary<string, string> ConnectedIds = new Dictionary<string, string>();

        }
        Thread Simulate; 
        public void sendMessage(string name, string message)
        {
            var Subscribers = from client in UserHandler.ConnectedIds
                              where (string)client.Value==name
                              select client;
            foreach(KeyValuePair<string,string>kp in Subscribers)
            {
                string ToId= kp.Key;
                Clients.Client(ToId).addMessage(name,message); 
            } 
        }
        public void Send(string name, string message)
        { 
            Clients.All.addMessage(name, message);
        }
        public void userConnected(string name)
        {
             
           
            string message = "用户 " + name + " 登录"; 
            //发送信息给其他人 
            //Clients.Others.addList(Context.ConnectionId, name); 
            //Clients.Others.hello(message);  
            //发送信息给自己，并显示上线清单 
            //Clients.Caller.getList(UserHandler.ConnectedIds.Select(p => new { id = p.Key, name = p.Value }).ToList());  
            //新增目前使用者上线清单 
            UserHandler.ConnectedIds.Add(Context.ConnectionId, name);
        }

        
        bool IsRun = false;
        public void SIMULATE()
        {
            if (!IsRun)
                IsRun = true;
            int i = 0;
            while (true)
            {
                
                i = i + 1;

                try
                {
                    Clients.All.addMessage(i.ToString(), i.ToString());
                    /*
                    var Subscribers = from client in VSP.Clients
                                      select client;
                    foreach (KeyValuePair<string, AMClient> client in Subscribers)
                    {
                        foreach (KeyValuePair<string, string> kp in client.Value.SubscribedValue)
                        {
                            client.Value.Send(kp.Value, i.ToString());
                        }
                    }*/
                }
                catch (Exception ex)
                {

                }
                if (i > 1000)
                    i = 0;

                Thread.Sleep(1000);
            }
        }
        public override Task OnConnected()
        {

            if (!IsRun)
            {
                Simulate = new Thread(SIMULATE);
                Simulate.Name = Guid.NewGuid().ToString();
                Simulate.IsBackground = true;
                Simulate.Start();
            }
            /*
            //Use Application.Current.Dispatcher to access UI thread from outside the MainWindow class
            Application.Current.Dispatcher.Invoke(() =>
                ((MainWindow)Application.Current.MainWindow).WriteToConsole("Client connected: " + Context.ConnectionId));
            */
            return base.OnConnected();
        }
        public override Task OnDisconnected(bool stopCalled)
        {
            /*
            //Use Application.Current.Dispatcher to access UI thread from outside the MainWindow class
            Application.Current.Dispatcher.Invoke(() =>
                ((MainWindow)Application.Current.MainWindow).WriteToConsole("Client disconnected: " + Context.ConnectionId));
            */
            return base.OnDisconnected(stopCalled);
        }

    }

}